package cn.flying.cloud.system.view;

import javax.annotation.Resource;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.flying.cloud.system.common.enums.TypeEnum;
import cn.flying.cloud.system.dict.entity.SysDictData;
import cn.flying.cloud.system.dict.entity.SysDictType;
import cn.flying.cloud.system.dict.service.SysDictDataService;
import cn.flying.cloud.system.dict.service.SysDictTypeService;


/**
 * 字典管理前端控制器
 *
 * @description:
 * @author: admin
 * @date: 2022年05月25日 19:21
 * @version: 1.0
 */
@Controller
@RequestMapping("/sys/dict/")
public class SysDictViewController {

    @Resource
    private SysDictDataService sysDictDataService;
    @Resource
    private SysDictTypeService sysDictTypeService;

    /**
     * 列表页面
     *
     * @return
     */
    @RequestMapping("type/index")
    public String dictTypeList(Model model) {
        SysDictData dictData = new SysDictData();
        dictData.setDictType(TypeEnum.TYPE_ENUM_STATUS.getType());
        List<SysDictData> statusList = sysDictDataService.getList(dictData);
        model.addAttribute("statusList", statusList);
        List<SysDictType> dictTypeList = sysDictTypeService.getList(new SysDictType());
        model.addAttribute("dictTypeList", dictTypeList);
        return "sys/dict/type/index";
    }

    /**
     * 新增/修改页面
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("type/turn/edit")
    public String turnTypeEdit(Long id, Model model) {
        SysDictData dictData = new SysDictData();
        dictData.setDictType(TypeEnum.TYPE_ENUM_STATUS.getType());
        List<SysDictData> statusList = sysDictDataService.getList(dictData);
        model.addAttribute("statusList", statusList);
        if (null != id) {
            SysDictType dictType = sysDictTypeService.getById(id);
            model.addAttribute("dictType", dictType);
        } else {
            SysDictType dictType = new SysDictType();
            dictType.setStatus("Y");
            model.addAttribute("dictType", dictType);
        }
        return "sys/dict/type/edit";
    }

    /**
     * 列表页面
     *
     * @return
     */
    @RequestMapping("code/index")
    public String dictCodeList(String dictType, Model model) {
        SysDictData dictData = new SysDictData();
        dictData.setDictType(TypeEnum.TYPE_ENUM_STATUS.getType());
        List<SysDictData> statusList = sysDictDataService.getList(dictData);
        model.addAttribute("statusList", statusList);
        model.addAttribute("dictType", dictType);
        return "sys/dict/code/index";
    }

    /**
     * 新增/修改页面（类型）
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("code/turn/edit")
    public String turnCodeEdit(Long id, String dictType, Model model) {
        if (null != id) {
            SysDictData dictData = sysDictDataService.getById(id);
            model.addAttribute("dictCode", dictData);
        } else {
            SysDictData dictData = new SysDictData();
            dictData.setDictType(dictType);
            dictData.setStatus("Y");
            model.addAttribute("dictCode", dictData);
        }
        return "sys/dict/code/edit";
    }

//    /**
//     * 获取所有的枚举类型
//     *
//     * @param
//     * @return
//     */
//    @PostMapping("keys")
//    @ResponseBody
//    public Rt<?> keys() {
//        return Rt.success(sysDictTypeService.keys());
//    }

    /**
     * 枚举下拉
     *
     * @param type
     * @return
     */
    @RequestMapping("/code/getCodeList")
    @ResponseBody
    public List<SysDictData> getCodeList(String type) {
        SysDictData dictData = new SysDictData();
        dictData.setDictType(type);
        return sysDictDataService.getList(dictData);
    }
}
